package com.view;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;

import ssu.crypto.Aes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.controller.Codec;
import com.controller.PacketController;
import com.model.MessageEnvelope;
import com.model.Packet;
import com.model.PacketHeader;
import com.model.ProtocolConstants;
import com.server.*;

public class LoginActivity extends Activity implements OnClickListener {
	private String sId, sPwd;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Button login;
		EditText id;
		EditText pwd;
		login = (Button) findViewById(R.id.login_login);
		id = (EditText) findViewById(R.id.login_id);
		pwd = (EditText) findViewById(R.id.login_pwd);
		id.setText("user1");
		pwd.setText("1234");
		sId = id.getText().toString();
		sPwd = pwd.getText().toString();
		login.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		Aes aes = null;
		byte[] key;
		byte[] sendData;
		Packet sendPacket;
		key = "INrWXyrD3KCIOTr5".getBytes(Charset.forName("ASCII"));
		aes = new Aes(key);
		Codec codec = new Codec();
		PacketHeader ph = null;
		OutputStream out;
		Socket cSocket;
		SocketConnection socketBridge = new SocketConnection();// 클라이언트
									// 소켓을
									// 연결하는
									// 다리
		byte[] eId, ePwd;
		try {
			eId = aes.ssu_encrypt(sId.getBytes(Charset.forName("US-ASCII")));
			ePwd = aes.ssu_encrypt(sPwd.getBytes(Charset.forName("US-ASCII")));
			Log.d("#####암호화된 아이디 : ", new String(eId));
			Log.d("#####암호화된 패스워드 : ", new String(ePwd));
			Log.d("#####암호화된 아이디  길이 : ", Integer.toString(eId.length));
			Log.d("#####암호화된 아이디  길이 : ", Integer.toString(ePwd.length));
			cSocket = socketBridge.getClietSocket();
			Log.d("#####전송 상태 : ","전송 완료");
			PacketController pc = new PacketController();
			MessageEnvelope me = new MessageEnvelope();
			me.setId(eId);
			me.setPassword(ePwd);
			sendData = codec.encodeMessageEnvelope(me, ProtocolConstants.MSG_LOGIN_REQ);
			ph = new PacketHeader(ProtocolConstants.PROTOCOL, ProtocolConstants.MSG_LOGIN_REQ, ProtocolConstants.MSG_LOGIN_ID_SIZE
					+ ProtocolConstants.MSG_LOGIN_PWD_SIZE);
			sendPacket = new Packet(ph,sendData);
			out = cSocket.getOutputStream();
			out.write(sendPacket.getBytes());
			//다음으로 정상적으로 읽었을 때 인텐트 넘겨줌
			cSocket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("#####소켓 에러",e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("#####소켓 에러",e.toString());
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("#####소켓 에러",e.toString());
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("#####소켓 에러",e.toString());
		}
		/**
		 * 모든 트랜잭션 수행 후, 뷰를 넘겨버린다.
		 */
		Intent intent = new Intent(LoginActivity.this, MainMenuActivity.class);
		startActivity(intent);
	}
}
